//
//  BaseViewController.m
//  wook
//
//  Created by guojiang on 5/8/14.
//  Copyright (c) 2014年 guojiang. All rights reserved.
//

#import "BaseViewController.h"
#import "IQKeyboardManager.h"

@interface BaseViewController ()
@property (nonatomic, strong) ResultTipView *networkTipView;
@end

@implementation BaseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.routerHelper = [[RouterHelper alloc] init];
    
    [self.routerHelper canConnectedInternetBlock:^(BOOL connected) {
        if (connected)
        {
            NSLog(@"connected:");
        }
        else
        {
            NSLog(@"un connected:");
        }
    }];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    

    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {self.edgesForExtendedLayout= UIRectEdgeNone;    }
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSInteger count  = [self.navigationController.viewControllers count];
    if (count > 1)
    {
        [self addLeftBarButtonWithNormalImageName:@"nav_back" selectedImageName:@"nav_back_pre" target:self action:@selector(backButtonPressed)];
    }

}

- (void)backButtonPressed
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)addLeftBarButton:(NSString *)title target:(id)target action:(SEL)action
{
    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:title
                                                               style:UIBarButtonItemStylePlain
                                                              target:self
                                                              action:action];
    [button setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = button;
}

-(void)addRightBarButton:(NSString *)title target:(id)target action:(SEL)action
{
    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:title
                                                               style:UIBarButtonItemStylePlain
                                                              target:self
                                                              action:action];
    self.navigationItem.rightBarButtonItem = button;
}



- (void)addLeftBarButtonWithNormalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName target:(id)target action:(SEL)action
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *img = [UIImage imageNamed:normalImageName];
    btn.frame = CGRectMake(0, 0, img.size.width, img.size.height);
    
    [btn setImage:[UIImage imageNamed:normalImageName] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateHighlighted];
    [btn setImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateSelected];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = buttonItem;
    

}

- (void)addRightBarButtonWithNormalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName target:(id)target action:(SEL)action
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *img = [UIImage imageNamed:normalImageName];
    btn.frame = CGRectMake(0, 0, img.size.width, img.size.height);
    
    [btn setImage:[UIImage imageNamed:normalImageName] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateHighlighted];
    [btn setImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateSelected];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = buttonItem;
}


- (void)pushViewControllerWithClassName:(NSString *)className
{
    Class class  = NSClassFromString(className);
    if (class) {
        UIViewController *vc = [class new];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        NSLog(@"类不存在...");
    }

}

#pragma mark 启动键盘通知

-(void)enableKeyboardNotification
{
    [[IQKeyboardManager sharedManager] disableInViewControllerClass:[self class]];
    [[IQKeyboardManager sharedManager] disableToolbarInViewControllerClass:[self class]];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
}

-(void)keyboardWillShow:(NSNotification *)noti
{
    CGSize size = [[[noti userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    
    [self keyboardWillShowKeyHeight:size.height];
}

-(void)keyboardWillHide
{
    self.view.frame = Rect(0, 64, self.view.frame.size.width, self.view.frame.size.height);
}

-(void)keyboardWillShowKeyHeight:(CGFloat)keyHeight
{
    
}

-(void)setKeyboardShowView:(UIView *)showView keyboardHeight:(CGFloat)keyHeight
{
    CGFloat distanceToMove = keyHeight- (ScreenHeight - CGRectGetMaxY(showView.frame));
    
    if (distanceToMove <= 0) {
        return;
    }
    
    CGRect rect = self.view.frame;
    rect.origin.y = -distanceToMove;
    
    [UIView animateWithDuration:0.3f animations:^{
        self.view.frame = rect;
    }];
}




#pragma mark --提示消息
//弹出消息
-(void)promptMessage:(NSString *)text
{
    [self promptMessage:text view:self.view];
}
-(void)promptMessage:(NSString *)text state:(ActivityState)state
{
    [self promptMessage:text view:self.view state:state];
}
-(void)promptMessage:(NSString *)text view:(UIView *)view
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = text;
    hud.margin = 10.f;
    hud.dimBackground=NO;
    
    [hud hide:YES afterDelay:2.f];
}
-(void)promptMessage:(NSString *)text view:(UIView *)view state:(ActivityState)state
{
    NSString *imageName =@"";
    if (state == ActivityStateSuccess) {
        imageName = @"checkmark";
    }else if (state == ActivityStateFailure){
        imageName = @"checkmark_failed";
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    hud.labelText = text;
    hud.margin = 10.f;
    
    [hud hide:YES afterDelay:2.f];
}
//开始加载提示
- (MBProgressHUD *)startActivityWithTitle:(NSString *)text
{
    return [self startActivityWithTitle:text view:self.view];
}

- (MBProgressHUD *)startActivityWithTitle:(NSString *)text view:(UIView *)view{
    self.activity.userInteractionEnabled=NO;
    [self.activity hide:YES];
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
    hud.labelText = text;
    hud.dimBackground= NO;
    hud.removeFromSuperViewOnHide = YES;
    [view addSubview:hud];
    [hud show:YES];
    return hud;

}



//停止加载提示
- (void)stopActivityWithActivity:(MBProgressHUD *)activity title:(NSString *)text{
    if (text != nil && ![text checkEmpty]) {
        activity.mode = MBProgressHUDModeText;
        activity.labelText = text;
        [activity hide:YES afterDelay:2.f];
    }else{
        [activity hide:YES];
    }
    self.activity = nil;
}

- (void)stopActivityWithActivity:(MBProgressHUD *)activity title:(NSString *)text afterDelay:(NSTimeInterval)delay
{
    if (text != nil && ![text checkEmpty]) {
        activity.mode = MBProgressHUDModeText;
        activity.labelText = text;
        [activity hide:YES afterDelay:delay];
    }else{
        [activity hide:YES];
    }
    self.activity = nil;
}


-(void)stopActivityWithActivity:(MBProgressHUD *)activity title:(NSString *)text state:(ActivityState)state
{
    NSString *imageName =@"";
    if (state == ActivityStateSuccess) {
        imageName = @"checkmark";
    }else if (state == ActivityStateFailure){
        imageName = @"checkmark_failed";
    }
    activity.mode = MBProgressHUDModeCustomView;
    activity.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    activity.labelText = text;
    [activity hide:YES afterDelay:2.f];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    self.networkTipView = nil;
    
    NSLog(@"%@ 调用了dealloc",[self class]);
}



- (void)showNetworkExceptionWithView:(UIView *) toView andTitle:(NSString *)title andButtonTitle:(NSString *)buttonTitle andBtnBlock:(ActionButtonClicked) refreshBtnClick{
    if ([toView isKindOfClass:[UITableView class]]) {
        self.networkTipView = [[ResultTipView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(toView.frame), CGRectGetHeight(toView.frame))];
        [toView addSubview:self.networkTipView];
    }else{
        self.networkTipView = [[ResultTipView alloc] init];
        _networkTipView.translatesAutoresizingMaskIntoConstraints = NO;
        [toView addSubview:self.networkTipView];
        
        
        [_networkTipView makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(toView);
        }];
    }
    _networkTipView.title =  title;//@"您还没有绑定任何一个小叶WIFI设备\n还没有小叶WIFI？赶快去买一个吧!";
    _networkTipView.buttonTitle = buttonTitle;// @"刷新试试";
    _networkTipView.buttonClicked =refreshBtnClick;
    [_networkTipView resetViewContent];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/





@end
